package com.tepth.remote.control.delegate.apply.examination;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.View;

import com.tepth.remote.control.R;
import com.tepth.remote.control.R2;
import com.tepth.remote.control.base.bean.ApplyHistoryBean;
import com.tepth.remote.control.base.mvp.BaseMvpDelegate;
import com.tepth.remote.control.manager.ConfigManager;
import com.tepth.remote.ui.alert.DialogLoader;
import com.tepth.remote.ui.alert.IDialogClickListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/23
 */
@SuppressLint("SetTextI18n")
public class ExaminationDelegate extends BaseMvpDelegate<ExaminationPresenter> implements ExaminationContract.View {

    public static final String TARGET = "target";
    public static final String CONTEXT = "context";
    public static final String APPLY = "applyHistoryBean";

    @BindView(R2.id.icon_toolbar_title)
    AppCompatTextView toolbarTitle = null;
    @BindView(R2.id.examination_context)
    AppCompatTextView textContext = null;
    @BindView(R2.id.edit_reason)
    AppCompatEditText editReason = null;
    @BindView(R2.id.examination_opinion)
    AppCompatTextView textOpinion = null;
    @BindView(R2.id.examination_result)
    AppCompatTextView textResult = null;
    @BindView(R2.id.linear_examination_close)
    LinearLayoutCompat linearClose = null;
    @BindView(R2.id.linear_examination_operate)
    LinearLayoutCompat linearOperate = null;

    private int targetId;
    private DialogLoader mDialogLoader = null;

    public static ExaminationDelegate create(Bundle bundle) {
        final ExaminationDelegate contentDelegate = new ExaminationDelegate();
        contentDelegate.setArguments(bundle);
        return contentDelegate;
    }

    @OnClick({R2.id.icon_toolbar_back, R2.id.linear_examination_agree, R2.id.linear_examination_refuse, R2.id.linear_examination_close})
    void onClick(View view) {
        final int id = view.getId();
        String reason = "";
        if (editReason != null && editReason.getText() != null) {
            reason = editReason.getText().toString();
        }
        if (id == R.id.icon_toolbar_back) {
            pop();
        } else if (id == R.id.linear_examination_agree) {
            mPresenter.onExamination(ConfigManager.getInstance().getUid(), targetId, 1, reason);
        } else if (id == R.id.linear_examination_refuse) {
            mPresenter.onExamination(ConfigManager.getInstance().getUid(), targetId, 2, reason);
        } else {
            pop();
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_examination;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        toolbarTitle.setText("请假审批");
        mDialogLoader = new DialogLoader(getProxyActivity());
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        final Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.getParcelable(APPLY) != null) {
                final ApplyHistoryBean bean = bundle.getParcelable(APPLY);
                if (bean != null) {
                    targetId = bean.getAutoId();
                    textContext.setText(bean.getContext());
                    if (bean.getAuditingType() == 1 || bean.getAuditingType() == 2) {
                        textOpinion.setText("审批意见：" + bean.getAuditingReason());
                        textResult.setText("处理结果：" + bean.getAuditingTypeName());
                        editReason.setVisibility(View.GONE);
                        textResult.setVisibility(View.VISIBLE);
                        linearClose.setVisibility(View.VISIBLE);
                        linearOperate.setVisibility(View.GONE);
                    } else {
                        textOpinion.setText("审批意见");
                        editReason.setVisibility(View.VISIBLE);
                        textResult.setVisibility(View.GONE);
                        linearClose.setVisibility(View.GONE);
                        linearOperate.setVisibility(View.VISIBLE);
                    }
                }
            } else {
                final String context = bundle.getString(CONTEXT);
                targetId = bundle.getInt(TARGET);
                textContext.setText(context);
                textOpinion.setText("审批意见");
                editReason.setVisibility(View.VISIBLE);
                textResult.setVisibility(View.GONE);
                linearClose.setVisibility(View.GONE);
                linearOperate.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public ExaminationPresenter getPresenter() {
        return new ExaminationPresenter(this);
    }

    @Override
    public void examinationSuccess(int type) {
        mDialogLoader.stopProgressWithSuccess((type == 1 ? "同意" : "拒绝") + "请假审批成功", "", new IDialogClickListener() {
            @Override
            public void onClick() {
                mDialogLoader.clearDialog();
                pop();
            }
        });
    }

    @Override
    public void examinationFailed(int code, String msg) {
        mDialogLoader.stopProgressWithFailed("审批失败", "");
    }

    @Override
    public void showLoading(String str) {
        mDialogLoader.showProgressDialog(false, "提示", str);
    }

    @Override
    public void hideLoading() {

    }
}
